<?php
class Blog extends Controller
{
	function __construct()
	{	
		parent::Controller();
		$this->load->model('mdl_blog_frontend');
		$this->template->set();
		$this->load->helper('smiley');
		ob_start();
	}
	
	function blog ()
	{
		self::__construct();
	}
	
	function index()
	{
		$posts = $this->mdl_blog_frontend->get_posts(10);
		$this->load->helper('date');
		
		$i = 0;
		$limit = 225;
		$break = " ";
		
		$header['title'] = "Blog";
		
		if ($posts !== FALSE)
		{
			
			foreach ($posts as $post)
			{
				
				
				$o[$i]['id'] = $post['id'];
				$o[$i]['title'] = $post['title'];
				$pad = '...<br />'.'<div class="readmore">'.anchor('blog/story/'.$post['id'], 'Read More...').'</div>';
				
				$o[$i]['content'] = $this->functions->summarise($post['content'], " ", $pad, $limit, 1, 0);
				$o[$i]['author'] = $post['author'];
				$o[$i]['timestamp'] = $post['timestamp'];
				$o[$i]['human_timestamp'] = unix_to_human($post['timestamp']);
				
				
				$i++;
			}

			$data['posts'] = $o;
			
			$this->template->load('header', $header);
			$this->template->load('blog/blog_frontpage', $data);
			$this->template->load('footer');
		}
		else
		{
			$this->template->load('header', $header);
			$this->template->load('blog/no_posts');
			$this->template->load('footer');
		}
	}
	
	function story ()
	{
		$id = $this->uri->segment(3);

		$post = $this->mdl_blog_frontend->get_post($id);
		$header = "";
		$header['title'] = "Blog - ". $post['title'];
		
		if ($post !== FALSE)
		{
			$this->template->load('header', $header);
			$this->template->load('blog/story', $post);
			$this->template->load('footer');
		}
		else
		{
			$this->template->load('header', $header);
			$this->template->load('blog/no_posts');
			$this->template->load('footer');
		}
	}
	
	function add_comment($postid=null){
		if (!$postid){
			die("No Post ID");
		}
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('name', 'Name', 'required|xss_clean|encode_php_tags');
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email|xss_clean|encode_php_tags');
		$this->form_validation->set_rules('title', 'Title', 'required|xss_clean|encode_php_tags');
		$this->form_validation->set_rules('message', 'Message', 'required|xss_clean|encode_php_tags|strip_image_tags');
		
		if ($this->form_validation->run() == FALSE){
			$post = $this->mdl_blog_frontend->get_post($postid);
			$header = ""; $header['title'] = "Blog - ". $post['title'];
			$this->template->load('header', $header);
			$this->template->load('blog/story', $post);
			$this->template->load('footer');
		} else {
			$this->mdl_blog_frontend->add_comment(
				$postid, 
				$this->input->post('name'), 
				$this->input->post('email'), 
				$this->input->post('title'), 
				$this->input->post('message')
			);
			
			$message['message'] = "Comment added.";
			$this->template->load('header');
			$this->template->load('blog/landing', $message);
			$this->template->load('footer');
		}
	}
	
	function all_posts ()
	{
		$posts = $this->mdl_blog_frontend->get_posts();
		$this->load->helper('date');
		
		$i = 0;
		$limit = 225;
		$break = " ";
		
		
		$header['title'] = "Blog - All Posts";
		
		if ($posts !== FALSE)
		{
			foreach ($posts as $post)
			{
				$o[$i]['id'] = $post['id'];
				$o[$i]['title'] = $post['title'];
				$pad = '...<br />'.'<div class="readmore">'.anchor('blog/story/'.$post['id'], 'Read More...').'</div>';
				
				$o[$i]['content'] = $this->functions->summarise($post['content'], " ", $pad, $limit, 1, 0);
				$o[$i]['author'] = $post['author'];
				$o[$i]['timestamp'] = $post['timestamp'];
				$o[$i]['human_timestamp'] = unix_to_human($post['timestamp']);
				$i++;
			}
			
			$data['posts'] = $o;		
			
			$this->template->load('header', $header);
			$this->template->load('blog/blog_frontpage', $data);
			$this->template->load('footer');
		}
		else
		{
			$this->template->load('header', $header);
			$this->template->load('blog/no_posts');
			$this->template->load('footer');
		}
	}
}
